package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.easy.tool.common.HttpUtil;
import org.easy.tool.entity.Company;
import org.easy.tool.mapper.CompanyMapper;
import org.easy.tool.service.ICompany;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Valley
 * @since 2024-02-27 20:20:11
 */
@Service
public class CompanyService extends ServiceImpl<CompanyMapper, Company> implements ICompany {

    public static final String BASE_URL = "http://api.waizaowang.com";
    public static final String TOKEN = "8cceb2ab595445e804622b6a9f63e824";
    public static final ObjectMapper MAPPER = new ObjectMapper();


    /**
     * 功能：根据股票code获取股票公司
     */
    public Company getCompany(String code) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Company::getCode, code);
        return this.getOne(wrapper);
    }


    /**
     * 功能：刷新股票简要信息数据库数据
     */
    @Override
    public List<LinkedHashMap> refreshData() throws Exception{
        List<LinkedHashMap> stockList = getSimpleStockList();
        List<Company> companies = new ArrayList<>();
        for (LinkedHashMap map : stockList) {
            String code = (String) map.get("code");
            String name = (String) map.get("name");
            String bk = (String) map.get("bk");
            Double roe = (Double) map.get("roe");
            Long marketValue = 0L;
            Object zsz = map.get("zsz");
            if (zsz instanceof Long){
                marketValue =  (Long) zsz;
            } else if (zsz instanceof Integer) {
                int tempValue = (Integer) zsz;
                marketValue = (long) tempValue;
            }

            String ipoTime = (String) map.get("ssdate");
            String detail = (String) map.get("z53");
            Company company = new Company();
            company.setCode(code);
            company.setName(name);
            company.setBlock(bk);
            company.setRoe(roe);
            company.setMarketValue(marketValue);
            company.setIpoTime(ipoTime);
            company.setDetail(detail);
            companies.add(company);
        }
        this.saveBatch(companies);
        return stockList;

    }

    /**
     * 功能：获取所有股票简要信息
     */
    public static List<LinkedHashMap> getSimpleStockList() throws Exception {
        String url = BASE_URL + "/doc/getBaseInfo" +
                "?type=1" +
                "&code=all" +
                "&fields=all" +
                "&export=1" +
                "&token=" + TOKEN;
        String resData = HttpUtil.get(url);
        LinkedHashMap resMap = MAPPER.readValue(resData, LinkedHashMap.class);
        List<LinkedHashMap> data = (List<LinkedHashMap>) resMap.get("data");
        System.out.println(data.size());
        return data;
    }



}
